package com.ehi.common.config;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 * Created by portz on 6/13/2017.
 */
@Configuration
@ComponentScan("com.ehi.common")
public class PropertyConfig {
    private static Logger log = LoggerFactory.getLogger(PropertyConfig.class);

    //@Bean
    public static PropertyPlaceholderConfigurer placeHolderConfigurer() {
        String extPropPath = System.getProperty("ext.prop.path");
        log.info("ext.prop.path:{}", extPropPath);
        PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer();
        props.setIgnoreResourceNotFound(true);
        props.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
        try {
            //props.setLocations(new ClassPathResource("application.properties"),new ClassPathResource("my.properties"));
            Resource[] resource4ClassPath = new PathMatchingResourcePatternResolver().getResources("classpath*:*.properties");
            Resource[] resource4File = null;
            if (StringUtils.isNotEmpty(extPropPath)) {
                String path = "file:/" + extPropPath + "**/*.properties";
                resource4File = new PathMatchingResourcePatternResolver().getResources(path);
            }
            for (Resource resource : resource4File) {
                System.out.println("==============" + resource.getFilename());
            }
            props.setLocations(ArrayUtils.addAll(resource4ClassPath, resource4File));
        } catch (Exception e) {
            log.error("Get Property file error:", e);
        }
        return props;
    }
}
